const { Service } = require("egg");

class UserService extends Service {
    /**
     * 根据用户名，查询该用户信息
     * @param {string} username 用户名
     */
    async findByUsername(username) {
        let sql = "select * from user where username = ?";
        let params = [username];
        let res = await this.app.mysql.query(sql, params);
        return res[0];
    }

    /**
     * 添加用户
     * @param {obejct} param0 要保存的用户信息
     */
    async create({ username, password, nickname }) {
        let sql =
            "insert into user (username, password, nickname) values (?, ?, ?)";
        let params = [
            username,
            this.ctx.helper.passwordEncoder(password),
            nickname,
        ];
        let res = await this.app.mysql.query(sql, params);
        return res;
    }
}

module.exports = UserService;
